#!/usr/bin/env bash

# chkconfig:         2345 75 15
# description:       redis服务器

LOG_PATH="../log"
#LOG_PATH="/var/log"
PID_PATH="/var/run"
TRIB="bin/redis-trib.rb"
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do 
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" 
done
CUR_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

cd "$CUR_DIR"

err_quit(){ if [ $1 -ne 0 ]; then echo $2; exit 1; fi;}

install_cluster(){
  start=7001
  end=7008
  for pm in "$@"; do
    case "$pm" in
      --start=*)
        start=${pm#*=};;
      --end=*)
        end=${pm#*=};;
    esac
  done
  expr $start + 0 >/dev/null 2>&1 || err_quit 1 "参数 1 必须为数字"
  expr $end + 0 >/dev/null 2>&1 || err_quit 1 "参数 2 必须为数字"
  [ $end -le $start ] && err_quit 1 "参数 2 需大于参数 1"
  [ $(expr $end - $start) -gt 10 ] && err_quit 1 "端口号不能超过 10 个"
  echo "开始创建集群 端口 $start --> $end"
  for (( i=$start; i<= $end; i++ )); do
    create "$i"
  done
}

create(){
  port=$1
  dir="redis-$port"
  [ -d "$dir" ] && err_quit 1 "端口: $port 已经存在"
  mkdir "$dir"
  sed -e "s@^\s*bind.*@bind 0.0.0.0@" \
    -e "s@^\s*port.*@port $port@" \
    -e "s@^\s*daemonize.*@daemonize yes@" \
    -e "s@^\s*pidfile.*@pidfile $PID_PATH/redis-$port.pid@" \
    -e "s@^\s*#\?\s*cluster-enabled.*@cluster-enabled yes@" \
    -e "s@^\s*#\?\s*cluster-config-file.*@cluster-config-file ./nodes-$port.conf@" \
    -e "s@^\s*#\?\s*cluster-node-timeout.*@cluster-node-timeout 15000@" \
    -e "s@^\s*logfile.*@logfile \"$LOG_PATH/redis-$port.log\"@" \
    "bin/redis.template.conf" > "$dir/redis.conf"
  sed -e "s@\s*REDIS_PORT=.*@REDIS_PORT=\"$port\"@" \
    "bin/redis.template.sh" > "$dir/redis.sh"
  chmod +x "$dir/redis.sh"
}

uninstall(){
  cluster=($(ls -a -1 -I . -I ..|grep '^redis-[0-9]\+$'))
  [ ${#cluster[@]} -eq 0 ] && err_quit 1 "没有安装redis!"
  read -t10 -r -p "将会删除 ${cluster[*]} 共 ${#cluster[@]} 个redis,是否继续 [Y/N]?" confirm
  [[ ! "$confirm" =~ ^[Yy]$ ]] && err_quit 1 "忽略卸载"
  for redis in "${cluster[@]}"; do
    echo "删除 $redis"
    rm -rf "$redis"
  done
  rm -rf log
}

start(){
  echo $CUR_DIR
  cluster=($(ls -a -1 -I . -I ..|grep '^redis-[0-9]\+$'))
  [ ${#cluster[@]} -eq 0 ] && err_quit 1 "没有安装redis!"
  if [[ "$LOG_PATH" =~ ^/ ]]; then
    [ -d "$LOG_PATH" ] || mkdir -p "$LOG_PATH"
  else
    [ -d log ] || mkdir log
  fi
  for redis in "${cluster[@]}"; do
    echo "启动 $redis"
    $redis/redis.sh start
  done
}

stop(){
  cluster=($(ls -a -1 -I . -I ..|grep '^redis-[0-9]\+$'))
  [ ${#cluster[@]} -eq 0 ] && err_quit 1 "没有安装redis!"
  for redis in "${cluster[@]}"; do
    echo "停止 $redis"
    $redis/redis.sh stop
  done
}

cluster(){
  [[ "$1" =~ --ip=* ]] && ip="${1#*=}"
  [ -z "$ip" ] && err_quit 1 "请输入 ip"
  [[ "$ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || err_quit 1 "IP地址格式错误"
  cluster=($(ls -a -1 -I . -I ..|grep '^redis-[0-9]\+$'|sort|awk -F - "{print \"$ip:\"\$2}"))
  $TRIB create --replicas 1 ${cluster[@]}
}

help(){
  echo "
  # 集群初始化
  $0 cluster --ip=192.168.0.1
  # 安装 start: 开始端口号（非必填） end: 结束端口号 （非必填）
  $0 install --start=7001 --end=7008
  # 卸载
  $0 uninstall
  # 启动
  $0 start
  # 关闭
  $0 stop
"
}

case "$1" in
  install)
    shift
    install_cluster "$@";;
  uninstall)
    uninstall;;
  start)
    start;;
  stop)
    stop;;
  cluster)
    shift
    cluster "$@";;
  -h|--help|-\?)
    help;;
  *)
    echo "未知参数 \"$1\" 运行 $0 --help 查看帮助"
    exit 1;;
esac
